﻿using Cxuu.Common;
using Cxuu.Models;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Repository;

[AppService]
public class ArticleStatementRepository : BaseRepository<ArticleStatement, PetaPocoContext>
{

    public ArticleStatementRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

    /// <summary>
    /// 编辑使用 查询一条
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<EditArticleStatement> GetEditAsync(int? id)
    {
        Sql sql = Sql.Builder;
        sql.Select("statement.*,g.Groupname,user.Nickname,cate.Name as CateName")
            .From("cxuu_article_statement as statement")
            .LeftJoin("cxuu_article_cate as cate").On("statement.Cid = cate.Id");
        sql.LeftJoin("cxuu_admin_user user").On("statement.Uid=user.Id");
        sql.LeftJoin("cxuu_admin_group g").On("user.Gid=g.Id");
        sql.Where("statement.Id =@0", id);
        return await CreateDAO().FirstOrDefaultAsync<EditArticleStatement>(sql);
    }


    /// <summary>
    /// 根据CID 查询一条 用于判断当前栏目下有没有数据及查询最新一条信息内容
    /// </summary>
    /// <param name="cid"></param>
    /// <returns></returns>
    public async Task<ArticleStatement> GetOneByCidAsync(int cid)
    {
        Sql sql = Sql.Builder;
        sql.Select("*").From("cxuu_article_statement").Where("Cid =@0", cid).OrderBy("Id desc");
        return await CreateDAO().FirstOrDefaultAsync<ArticleStatement>(sql);
    }

    /// <summary>
    /// 获取列表 
    /// </summary>
    public async Task<Page<ViewArticleStatementAndCate>> GetListAsync(ArticleStatementQuery articleQuery)
    {
        Sql sql = Sql.Builder;
        sql.Select("cate.Name as CateName,dept.DeptName,u.Nickname,article.*")
            .From("cxuu_article_statement as article")
            .LeftJoin("cxuu_article_cate as cate").On("article.Cid = cate.Id");
        sql.LeftJoin("cxuu_admin_user u").On("article.Uid=u.Id");
        sql.LeftJoin("cxuu_admin_dept dept").On("u.DeptId=dept.Id");

        if (articleQuery.Uids == "own")
            sql.Where("article.Uid = @0", articleQuery.Uid);
        else if (articleQuery.Uids != null)
            sql.Where("article.Uid IN (" + articleQuery.Uids + ")");

        if (articleQuery.Cid != "0" && articleQuery.Cid != null)
            // sql.Where("article.Cid IN (" +articleQuery.Cid + ")");
            sql.Where("article.Cid =@0", int.Parse(articleQuery.Cid));

        if (!string.IsNullOrEmpty(articleQuery.Title))
            sql.Where("article.Title like @0", "%" + articleQuery.Title + "%");
        
        if (!string.IsNullOrEmpty(articleQuery.Clercator))
            sql.Where("article.Clercator like @0", "%" + articleQuery.Clercator + "%");

        if (!string.IsNullOrEmpty(articleQuery.NickName))
            sql.Where("u.Nickname like @0", "%" + articleQuery.NickName + "%");

        if (articleQuery.StartDate != null && articleQuery.EndDate != null)
            sql.Where("article.InsertTime between @0 AND @1", articleQuery.StartDate, articleQuery.EndDate);

        if (!string.IsNullOrEmpty(articleQuery.Orderby))
            sql.OrderBy("article." + articleQuery.Orderby);

        return await CreateDAO().PageAsync<ViewArticleStatementAndCate>(articleQuery.CurrentPage, articleQuery.Limit, sql);
    }


    /// <summary>
    /// 插入一条信息
    /// </summary>
    /// <param name="article"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public async Task<ArticleStatement> InsertArticleAndContentAsync(ArticleStatement  articleStatement)
    {
        var dao = CreateDAO();
        await dao.OpenSharedConnectionAsync();
        try
        {
            var a = await dao.InsertAsync(articleStatement);

            if (a != null)
                return articleStatement;
        }
        finally
        {
            dao.CloseSharedConnection();
        }
        return null;
    }
    /// <summary>
    /// 更新一条信息
    /// </summary>
    /// <param name="article"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public async Task<int> UpdateArticleAndContentAsync(EditArticleStatement  articleStatement)
    {
        if (articleStatement != null)
        {
            var dao = CreateDAO();
            ArticleStatement a = await dao.SingleOrDefaultAsync<ArticleStatement>(articleStatement.Id);
            a.Title = articleStatement.Title;
            a.Attments = articleStatement.Attments;
            a.Img = articleStatement.Img;
            a.ObjectId1 = articleStatement.ObjectId1;
            a.ObjectId2 = articleStatement.ObjectId2;
            a.Clercator = articleStatement.Clercator;
            a.Remarks = articleStatement.Remarks;
            return await dao.UpdateAsync(a);
        }
        return 0;
    }
}
